package com.cwx.mychat.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cwx.mychat.entity.constant.CodeConstant;
import com.cwx.mychat.entity.dto.TokenUserInfoDto;
import com.cwx.mychat.entity.enums.ExceptionBizCodeEnum;
import com.cwx.mychat.entity.po.UserContactCategory;
import com.cwx.mychat.exception.BusinessException;
import com.cwx.mychat.service.UserContactCategoryService;
import com.cwx.mychat.service.impl.UserContactCategoryServiceImpl;
import com.cwx.mychat.utils.R;
import com.cwx.mychat.utils.common.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 作者： cwx
 * 时间： 2024/12/11 17:25
 * 版本： 1.0
 * 注释：
 */
@RestController
@RequestMapping("/contactCategory")
public class UserContactCategoryController{

    @Autowired
    private UserContactCategoryService userContactCategoryService;
    /**
     * 获取所有分组
     * @return
     */
    @GetMapping("/getAll")
    public R getAll(){
        //获取当前用户
        TokenUserInfoDto tokenUserInfoDto = CommonUtil.getSecurityContext();
        List<UserContactCategory> userContactCategories = userContactCategoryService.getBaseMapper().selectList(new LambdaQueryWrapper<UserContactCategory>()
                .select(UserContactCategory::getCategoryId, UserContactCategory::getCategoryName, UserContactCategory::getSort)
                .eq(UserContactCategory::getUserId, tokenUserInfoDto.getUserId()));
        return R.ok().put(CodeConstant.RESULT_MSG, userContactCategories);
    }

    /**
     * 获取所有群聊分组
     * @return
     */
    @GetMapping("/getAllCategoryGroup")
    public R getAllCategoryGroup(){
        TokenUserInfoDto tokenUserInfoDto = CommonUtil.getSecurityContext();
        return userContactCategoryService.getAllGroupCategory(tokenUserInfoDto.getUserId());
    }

    /**
     * 添加分组
     * @param contactCategoryParam
     * @return
     */
    @PostMapping("/addGroupCategory")
    public R addGroupCategory(@RequestBody UserContactCategoryServiceImpl.ContactCategoryParam contactCategoryParam) throws BusinessException {
        return userContactCategoryService.addGroupCategory(contactCategoryParam);
    }

    /**
     * 删除分组
     * @param categoryId
     * @param userId
     * @return
     */
    @DeleteMapping("/deleteCategory")
    public R deleteCategory(@RequestParam("categoryId") String categoryId, @RequestParam("userId") String userId) throws BusinessException {
        if(!Objects.equals(userId, CommonUtil.getSecurityContext().getUserId())){
            throw new BusinessException(ExceptionBizCodeEnum.ACCOUNT_NOT_LOGIN);
        }
        userContactCategoryService.removeById(categoryId);
        return R.ok();
    }

    /**
     * 根据id获取分组信息
     * @param categoryId
     * @param userId
     * @return
     */
    @GetMapping("/getCategoryDetail")
    public R getCategoryById(@RequestParam("categoryId") String categoryId, @RequestParam("userId") String userId) throws BusinessException {
        if(!Objects.equals(userId, CommonUtil.getSecurityContext().getUserId())){
            throw new BusinessException(ExceptionBizCodeEnum.ACCOUNT_NOT_LOGIN);
        }
        UserContactCategory userContactCategory = userContactCategoryService.getOne(new LambdaQueryWrapper<UserContactCategory>().eq(UserContactCategory::getCategoryId, categoryId)
                .eq(UserContactCategory::getUserId, userId));
        return R.ok().put(CodeConstant.RESULT_MSG, userContactCategory);
    }

    public record ContactCategoryParam(String categoryId, String categoryName, String desc, Integer type, List<String> groupOrUserIds, String userId){}
    /**
     * 更新分组
     * @param contactCategoryParam
     * @return
     * @throws BusinessException
     */
    @PostMapping("/updateCategory")
    public R updateCategory(@RequestBody ContactCategoryParam contactCategoryParam) throws BusinessException {
        if(!Objects.equals(contactCategoryParam.userId(), CommonUtil.getSecurityContext().getUserId())){
            throw new BusinessException(ExceptionBizCodeEnum.ACCOUNT_NOT_LOGIN);
        }
        return userContactCategoryService.updateCategory(contactCategoryParam);
    }
}
